/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.io.File;
import java.util.*;
import service.MD5;

/**
 *
 * @author datateknik
 */
public class RandomFilename {
   
    public static String createFilename(String ext, String path) {
        
        String returnData = "";
        long timestamp = System.currentTimeMillis();
        returnData += timestamp;                        //get and add current time to string
        
        Random rand = new Random();
        returnData += rand.nextLong();                  //add a random long to string
        
        MD5 md5 = new MD5();
        returnData = md5.stringToMD5(returnData);       //get MD5 hash of returndata
                
        File file = new File(path+returnData+"."+ext);   
        if(file.exists())
            return createFilename(ext, path);                 //if filename exists create a new one, else return filename
        
        return returnData+"."+ext;
    }
}
